package escom.dds.swing.graficas;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.List;

import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.util.Rotation;

import escom.dds.modelo.Alumno;
import escom.dds.rmi.dataset.DataSet;
import escom.dds.rmi.dataset.IDataSet;

public class AlumnosXCarrera extends ApplicationFrame{

	  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public AlumnosXCarrera(String applicationTitle, String chartTitle) {
	        super(applicationTitle);
	        // This will create the dataset 
	        PieDataset dataset = createDataset();
	        // based on the dataset we create the chart
	        JFreeChart chart = createChart(dataset, chartTitle);
	        // we put the chart into a panel
	        ChartPanel chartPanel = new ChartPanel(chart);
	        // default size
	        chartPanel.setPreferredSize(new java.awt.Dimension(1000, 540));
	        // add it to our application
	        setContentPane(chartPanel);
	    }
	    
	    
	/**
	     * Creates a sample dataset 
	     */

	    private  PieDataset createDataset() {
	        try {
	        	DefaultPieDataset result = new DefaultPieDataset();
				IDataSet datos = (IDataSet)Naming.lookup("//localhost/dataset");
				List<Alumno> alumnos = datos.getAlumnos();
				
				int a=0, b=0, c=0;
				for(Alumno alumno : alumnos)
				{
					if(alumno.getIdCarrera()==1)
						a++;
					else
						if(alumno.getIdCarrera()==2)
							b++;
						else
							c++;
				}
				int total = a+b+c;
				result.setValue("ISC",a/total);
				result.setValue("ICE",b/total);
				result.setValue("LNI",c/total);
				return result;
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NotBoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        return new DefaultPieDataset();	        
	    }
	    
	    
	/**
	     * Creates a chart
	     */

	    private JFreeChart createChart(PieDataset dataset, String title) {
	        JFreeChart chart = ChartFactory.createPieChart3D(title,          // chart title
	            dataset,                // data
	            true,                   // include legend
	            true,
	            false);

	        PiePlot3D plot = (PiePlot3D) chart.getPlot();
	        plot.setStartAngle(290);
	        plot.setDirection(Rotation.CLOCKWISE);
	        plot.setForegroundAlpha(0.5f);
	        return chart;
	        
	    }
	    
   	   public static void main(String[] args) {
	    	          AlumnosXCarrera demo = new AlumnosXCarrera("Alumnos por Carrera", "¿Cuántos alumnos hay en cada carrera?");
	    	          demo.pack();
	    	          demo.setVisible(true);
	   }
	
}
